<?php 
namespace Home\Controller;
use Think\Controller;
class RegController extends CommonController
{
	public function index()
	{
		//print_r($_POST);
		$question = D('vercode')->vercode();
		$this->assign('question',$question);

		$this->assign('title','注册');
		$this->display();
	}
	//前端注册检测
	public function reg_Check()
	{
		// field:Object
		// email:"qq@qq.com"
		// pass:"aaaaaa"
		// repass:"aaaaaa"
		// username:"aaa"
		// vercode:"aa"
		$email = $_POST["email"];
		$pass = $_POST["pass"];
		$repass = $_POST["repass"];
		$username = $_POST["username"];
		$vercode = $_POST["vercode"];

		$preg_pass = "/^[0-9A-z]{6,16}$/";
		$pass_Yz = preg_match($preg_pass, $pass);
		if (!$pass_Yz) {
			$arr = array(
					"error" => 0,
					"info" => "密码必须是6~16位字符串"
				);
			echo json_encode($arr);
			exit;
		}
		if ($pass!=$repass) {
			$arr = array(
					"error" => 0,
					"info" => "确认密码输入有误"
				);
			echo json_encode($arr);
			exit;
		}

		$data = M('user')->select();
		foreach ($data as $v) {
			if ($v["email"]==$email) {
				$arr = array(
					"error" => 0,
					"info" => "邮箱已存在"
					);
				echo json_encode($arr);
				exit;
			}
		}

		if ($_SESSION['vercode_answer']) {
			if ($_SESSION['vercode_answer']!=$vercode) {
				$arr = array(
					"error" => 0,
					"info" => "人类问题错误"
					);
				echo json_encode($arr);
				exit;
			}
		}

		$arr = array(
				'error' =>1,
				'info'=>"注册中..."
				);
		echo json_encode($arr);
	}
	//注册
	public function reg()
	{
		//print_r($_POST);
		if ($_POST) {
			$data['create_time'] = time();
			$data['email'] = $_POST["email"];
			$data['username'] = $_POST["username"];
			$data['password'] = $_POST["pass"];
			$data['create_ip'] =  get_client_ip();
			$data['openid'] = sj_Rand(18);
		}
		//var_dump($data); 
		M('user')->add($data);
		$this->redirect('Home/login/index');
	}



}

?>